package lan.battletank.element;

import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * The class that defines the FPS element
 *
 * @author Yvn_pq
 */
public class FpsEntity {

	/** Percentage of the screen */
	private final float Y_RATIO = 0.1f;
	private final float X_RATIO = 0.005f;

	/** Paint setting */
	private final int FPS_COLOR = 0xffffffff;
	private final int FPS_TEXT_SIZE = 20;

	private Paint mFpsPaint;

	/** Fps Element Point */
	private float mX = 0;
	private float mY = 0;

	/** The text to view */
	private String mFpsText = "0";

	private int mRenderingCnt;

	/** Time to start the count */
	private long mStartTime;

	/**
	 * Constructor
	 */
	public FpsEntity() {
		mStartTime = System.currentTimeMillis();
		mRenderingCnt = 0;
		mFpsPaint = new Paint();
		mFpsPaint.setColor(FPS_COLOR);
		mFpsPaint.setTextSize(FPS_TEXT_SIZE);
		mFpsPaint.setAntiAlias(true);
		mFpsPaint.setDither(false);
	}

	/**
	 * Update the FPS
	 *
	 * @return String Fps
	 */
	public void updateFps() {
		// more than one second has elapsed
		if (999 < System.currentTimeMillis() - mStartTime) {
			mFpsText = String.valueOf(mRenderingCnt);
			mRenderingCnt = 0;
			mStartTime = System.currentTimeMillis();
		} else {
			mRenderingCnt++;
		}
	}

	/**
	 * sets the point
	 *
	 * @param canvasWidth
	 *            width of the screen
	 * @param canvasHeight
	 *            height of the screen
	 */
	public void setPositionAndInitialize(int canvasWidth, int canvasHeight) {
		mX = canvasWidth * X_RATIO;
		mY = canvasHeight * Y_RATIO;
	}

	/**
	 * draw
	 *
	 * @param canvas
	 */
	public void draw(Canvas canvas) {
		canvas.drawText("FPS:" + mFpsText, mX, mY, mFpsPaint);
	}

	public Paint getPaint() {
		return mFpsPaint;
	}

	public void setPaint(Paint paint) {
		this.mFpsPaint = paint;
	}
}
